var House = House || {};

House = function (camera,scene) {

			
			// this.centerOffset={x:1,y:1,z:1};
			// this.zmesh=null;
			// this.projector = new THREE.Projector();
			this.camera=camera;
			this.scene=scene;
			this.wallHight=240;
			this.wallThick=20;
			this.colliders=[];
			this.goods=[];
			this.objects=[];
			var me=this;
			this.hsId="";
			
};
House.prototype = {

	constructor:House,
	
	
	build:function(id,roomStyles){
			// plane = new THREE.Mesh( new THREE.PlaneGeometry( Number(roomStyles[0].width),Number(roomStyles[0].depth) ), new THREE.MeshBasicMaterial( { color: 0x444444 } ) );
        	// // plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0x444444} ) );
// 
        	// plane.position.x=Number(roomStyles[0].top);
        	// plane.position.z=Number(roomStyles[0].left);
        	// plane.position.y=Number(roomStyles[0].top);
        	// plane.rotation.x = - 90 * Math.PI / 180;
        	// plane.type=roomStyles[0].roomtype;
        	// this.scene.add( plane );
        	this.hsId=id;
		for (i in roomStyles){
	     	plane = new THREE.Mesh( new THREE.PlaneGeometry( Number(roomStyles[i].width),Number(roomStyles[i].depth) ), new THREE.MeshBasicMaterial( { color: 0x444444 } ) );
        	plane.position.x=Number(roomStyles[i].top);
        	plane.position.z=Number(roomStyles[i].left);
        	// plane.position.y=Number(roomStyles[i].top);
        	plane.rotation.x = - 90 * Math.PI / 180;
        	plane.type=roomStyles[i].roomtype;
        	plane.name='floor';
        	this.scene.add( plane );
        	

        	this.build4Walls(roomStyles);
        	
        // cubeGeo = new THREE.CubeGeometry( 200, 200, 200 );
        // cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff80, shading: THREE.FlatShading, map: THREE.ImageUtils.loadTexture( "textures/square-outline-textured.png" ) } );
        // cubeMaterial.color.setHSV( 0.1, 0.7, 1.0 );
        // cube = new THREE.Mesh( cubeGeo, cubeMaterial );
        // cube.name='cube';
        // cube.position.y=100;
        // this.scene.addObject( cube ) ;
    	}
	},
	build4Walls:function(roomStyles){
		
		 //left wall;
          cubeGeo=new THREE.CubeGeometry(this.wallThick/2,this.wallHight,Number(roomStyles[i].depth));
          cubeMaterial = new THREE.MeshLambertMaterial( { color:0x066666, opacity: 1, shading: THREE.FlatShading } );
          cube=new THREE.Mesh(cubeGeo,cubeMaterial);
          cube.position.z=Number(roomStyles[i].left);
          // plane.position.z=Number(roomStyles[i].top);
          cube.position.x=Number(roomStyles[i].top)-Number(roomStyles[i].width)/2+this.wallThick/4;
          cube.position.y=this.wallHight/2;
          cube.name='wall';
          // cube.rotation.x = - 90 * Math.PI / 180;
          this.scene.add(cube);
          

          //right wall;
          cubeGeo=new THREE.CubeGeometry(this.wallThick/2,this.wallHight,Number(roomStyles[i].depth));
          cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff80, opacity: 1,shading: THREE.FlatShading } );
          cube=new THREE.Mesh(cubeGeo,cubeMaterial);
          cube.position.z=Number(roomStyles[i].left);
          // plane.position.z=Number(roomStyles[i].top);
          cube.position.x=Number(roomStyles[i].top)+Number(roomStyles[i].width)/2-this.wallThick/4;
          cube.position.y=this.wallHight/2;
          cube.name='wall';
          // cube.rotation.x = - 90 * Math.PI / 180;
          this.scene.add(cube);
          // this.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
          
          //top wall;
          cubeGeo=new THREE.CubeGeometry(Number(roomStyles[i].width),this.wallThick/2,this.wallHight);
          cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff80, opacity: 1, shading: THREE.FlatShading } );
          cube=new THREE.Mesh(cubeGeo,cubeMaterial);
          cube.position.x=Number(roomStyles[i].top);
          // plane.position.z=Number(roomStyles[i].top);
          cube.position.z=Number(roomStyles[i].left)-Number(roomStyles[i].depth)/2+this.wallThick/4;
          cube.position.y=this.wallHight/2;
          cube.rotation.x = - 90 * Math.PI / 180;
          cube.name='wall';
          this.scene.add(cube);
          // this.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
          
          //buttom wall;
          cubeGeo=new THREE.CubeGeometry(Number(roomStyles[i].width),this.wallThick/2,this.wallHight);
          cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff80, opacity: 1, shading: THREE.FlatShading } );
          cube=new THREE.Mesh(cubeGeo,cubeMaterial);
          cube.position.x=Number(roomStyles[i].top);
          // plane.position.z=Number(roomStyles[i].top);
          cube.position.z=Number(roomStyles[i].left)+Number(roomStyles[i].depth)/2-this.wallThick/4;
          cube.position.y=this.wallHight/2;
          cube.rotation.x = - 90 * Math.PI / 180;
          cube.name='wall';
          this.scene.add(cube);
          // this.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
          // // this.colliders.push( THREE.CollisionUtils.MeshOBB(cube) );
//           
        // //           var ray = new THREE.Ray( sphere.position, new THREE.Vector3(0,0,1) );
        // // 
        // // var c = THREE.Collisions.rayCastNearest(ray);
	},
	save:function(saveHouseUrl){
  		
		$.post(saveHouseUrl,{goods:eval(this.goods),houseId:this.hsId},
  			function(data){
			// alert(data);
  		},"json");
  		
  		//这里返回的类型有：json,html,xml,text
  		
	},
	load:function(h,house,facility,scene){
		
		house.goods=[];
		house.objects=[];
		$.post(loadHouseUrl,{h:h},
  			function(data){
  				// // $('.house').html(data);
  				// console.warn(data);
  							house.build(data.hs._id.$id,data.hs.roomStyles);  			
  			for(i in data['gd']){
  				
  				facility.loadMesh(data['gd'][i],h['goods'][i],scene);
  				facility.locate(event);
				// gds={facility.goods.gid,position:facility.goods.position,rotation:facility.goods.rotation};
				house.goods.push({gid:facility.goods.gid,
								  position:{x:facility.goods.position.x,y:facility.goods.position.y,z:facility.goods.position.z},
								  rotation:{x:facility.goods.rotation.x,y:facility.goods.rotation.y,z:facility.goods.rotation.z}
								  });
  				console.warn(house.goods);
  			}
				//objects.push(facMesh);
				facility.initCursor(scene,$('.stage canvas'));
				//scene.add(facMesh);
			
  		},"json");
	},
}
		